#include "syssettingmodel.h"

#include "databasemanager.h"
#include "qdebug.h"
SysSettingModel * SysSettingModel::m_pInstance = NULL;
SysSettingModel *SysSettingModel::getInstance()
{
    if(m_pInstance == NULL)
    {
        m_pInstance = new SysSettingModel();
    }
    return m_pInstance;
}

SysSettingModel::SysSettingModel(QObject *parent)
    :QObject(parent)
{
    QString strErr;
    DatabaseManager::getInstance()->loadConfig(m_bShowVirtualBtns,m_bShowJoystickBtns,m_strTelemetryFolder,strErr);
    DatabaseManager::getInstance()->getUpperLimbCmd(m_upperLimbCmdList,strErr);
    DatabaseManager::getInstance()->getGlobalCmd(m_globalCmdList,strErr);
    DatabaseManager::getInstance()->getAudioFile(m_audioFileList,strErr);

}

QString SysSettingModel::strTelemetryFolder() const
{
    return m_strTelemetryFolder;
}


CMD_TYPE SysSettingModel::getCmdByName(QString strName, Command &cmd)
{
    for(int i = 0; i < m_upperLimbCmdList.size(); ++i)
    {
        if(m_upperLimbCmdList[i].strShowText.trimmed() == strName.trimmed())
        {
            cmd = m_upperLimbCmdList[i];
            return CMD_TYPE_UPPER;
        }
    }
    for(int i = 0; i < m_globalCmdList.size(); ++i)
    {
        if(m_globalCmdList[i].strShowText.trimmed() == strName.trimmed())
        {
            cmd = m_globalCmdList[i];
            return CMD_TYPE_GLOBAL;
        }
    }
    return CMD_TYPE_NONE;
}


CMD_TYPE SysSettingModel::getCmdByHotKey(FUNCTION_KEY nFuncKey, STICK_KEY nHotKey, Command &cmd)
{
    for(int i = 0; i < m_upperLimbCmdList.size(); ++i)
    {
        if(m_upperLimbCmdList[i].nFuncKey == nFuncKey && m_upperLimbCmdList[i].nHotKey == nHotKey)
        {
            cmd = m_upperLimbCmdList[i];
            return CMD_TYPE_UPPER;
        }
    }
    for(int i = 0; i < m_globalCmdList.size(); ++i)
    {
        if(m_globalCmdList[i].nFuncKey == nFuncKey && m_globalCmdList[i].nHotKey == nHotKey)
        {
            cmd = m_globalCmdList[i];
            return CMD_TYPE_GLOBAL;
        }
    }
    return CMD_TYPE_NONE;
}

bool SysSettingModel::getAudioByHotKey(FUNCTION_KEY nFuncKey, STICK_KEY nHotKey, AudioFile &file)
{
    for(int i = 0; i < m_audioFileList.size(); ++i)
    {
        if(m_audioFileList[i].nFuncKey == nFuncKey && m_audioFileList[i].nHotKey == nHotKey)
        {
            file = m_audioFileList[i];
            return true;
        }
    }
    return false;
}

QList<AudioFile> *SysSettingModel::audioFileList()
{
    return & m_audioFileList;
}

void SysSettingModel::setAudioFileList(QList<AudioFile> &newFileList)
{
    QString strErr;
    if( m_audioFileList != newFileList)
    {
        DatabaseManager::getInstance()->updateAudioFile(newFileList,strErr);
        m_audioFileList = newFileList;
        emit audioFileChanged();
    }
}

bool SysSettingModel::bShowVirtualBtns() const
{
    return m_bShowVirtualBtns;
}
bool SysSettingModel::bShowJoystickBtns() const
{
    return m_bShowJoystickBtns;
}

void SysSettingModel::setOtherConfig(bool bShowVirtualBtns,bool bShowJoystickBtns, QString strTelemetryFolder)
{
    bool bChanged = false;
    if(m_bShowVirtualBtns!= bShowVirtualBtns)
    {
        bChanged = true;
        m_bShowVirtualBtns = bShowVirtualBtns;
        emit showVirtualCtrlBtnChanged();
    }
    if(m_strTelemetryFolder!= strTelemetryFolder)
    {
        bChanged = true;
        m_strTelemetryFolder = strTelemetryFolder;
        emit strTelemetryFolderChanged();
    }
    if(m_bShowJoystickBtns!=bShowJoystickBtns){
        bChanged = true;
        m_bShowJoystickBtns = bShowJoystickBtns;
        emit showJoystickBtnChanged();
    }
    if(bChanged)
    {
        QString strErr;
        DatabaseManager::getInstance()->setConfig(bShowVirtualBtns,bShowJoystickBtns,strTelemetryFolder,strErr);
    }
}


QList<Command> *SysSettingModel::upperLimbCmdList()
{
    return &m_upperLimbCmdList;
}

QList<Command> SysSettingModel::upperLimbCmdList(int nParentID)
{
    QList<Command> ret;
    for(int i = 0; i < m_upperLimbCmdList.size(); ++i)
    {
        if(m_upperLimbCmdList[i].nParentID == nParentID)
        {
            ret.push_back(m_upperLimbCmdList[i]);
        }
    }
    return ret;
}

void SysSettingModel::setUpperLimbCmdList(QList<Command> &newCmdList)
{
    QString strErr;
    if( m_upperLimbCmdList != newCmdList)
    {
        DatabaseManager::getInstance()->updateUpperLimbCmd(newCmdList,strErr);
        m_upperLimbCmdList = newCmdList;
        emit upperLimbCmdChanged();
    }
}

Command *SysSettingModel::getUpperLimbCmd(int nCmdID)
{
    Command * pRet = NULL;
    for(int i = 0; i < m_upperLimbCmdList.size(); ++i)
    {
        if(m_upperLimbCmdList[i].nID == nCmdID )
        {
            pRet = &m_upperLimbCmdList[i];
            break;
        }
    }
    return pRet;
}

QList<Command> *SysSettingModel::globalCmdList()
{
    return &m_globalCmdList;
}

QList<Command> SysSettingModel::globalCmdList(int nParentID)
{
    QList<Command> ret;
    for(int i = 0; i < m_globalCmdList.size(); ++i)
    {
        if(m_globalCmdList[i].nParentID == nParentID)
        {
            ret.push_back(m_globalCmdList[i]);
        }
    }
    return ret;
}

void SysSettingModel::setGlobalCmdList(QList<Command> &newCmdList)
{
    QString strErr;
    if(m_globalCmdList != newCmdList)
    {
        DatabaseManager::getInstance()->updateGlobalCmd(newCmdList,strErr);
        m_globalCmdList = newCmdList;
        emit globalCmdChanged();
    }
}

Command *SysSettingModel::getGlobalCmd(int nCmdID)
{
    Command * pRet = NULL;
    for(int i = 0; i < m_globalCmdList.size(); ++i)
    {
        if(m_globalCmdList[i].nID == nCmdID)
        {
            pRet = &m_globalCmdList[i];
            break;
        }
    }
    return pRet;
}
